package rexsee.core.application;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;

public class Resource {

	private static final String DEFAULT_PREFIX = "rexsee";

	private static final String STRING_PREFIX = "app_prefix";
	private static final String STRING_HOME = "app_home";
	private static final String STRING_AUTHOR = "app_author";
	private static final String STRING_EMAIL = "app_email";
	private static final String STRINGARRAY_DOMAIN_WHITE_LIST = "allow_domains";
	private static final String DRAWABLE_COVER = "cover";

	private static final String STRING_TITLEBAR = "titleBar";
	private static final String STRING_STATUSBAR = "statusBar";
	private static final String STRING_FULLSCREEN = "fullScreen";
	private static final String STRING_PROGRESSCOVER = "progressCover";
	private static final String STRING_BROWSERPROGRESSDIALOG = "browserProgressDialog";
	private static final String STRING_BROWSERBACKGROUNDCOLOR = "browserBackgroundColor";
	private static final String STRING_BAIDUSTATISTIC = "baiduStatistic";

	private static final String STRING_SCREENORIENTATION = "screenOrientation";

	public final String prefix;
	public final String home;
	public final String author;
	public final String email;
	public final String[] domainWhiteList;
	public final Drawable cover;

	public final boolean titleBar;
	public final boolean statusBar;
	public final boolean fullScreen;
	public final boolean progressCover;
	public final boolean browserProgressDialog;
	public final String browserBackgroundColor;
	public final boolean baiduStatistic;

	public final String screenOrientation;

	public Resource(Context context) {

		String packageName = context.getPackageName();
		Resources r = context.getResources();

		int id;

		id = r.getIdentifier(STRING_PREFIX, "string", packageName);
		prefix = (id == 0) ? DEFAULT_PREFIX : r.getString(id);

		id = r.getIdentifier(STRING_HOME, "string", packageName);
		home = (id == 0) ? "" : r.getString(id);

		id = r.getIdentifier(STRING_AUTHOR, "string", packageName);
		author = (id == 0) ? "" : r.getString(id);

		id = r.getIdentifier(STRING_EMAIL, "string", packageName);
		email = (id == 0) ? "" : r.getString(id);

		id = r.getIdentifier(STRINGARRAY_DOMAIN_WHITE_LIST, "array", packageName);
		domainWhiteList = (id == 0) ? null : r.getStringArray(id);

		id = r.getIdentifier(DRAWABLE_COVER, "drawable", packageName);
		cover = (id == 0) ? null : r.getDrawable(id);

		id = r.getIdentifier(STRING_SCREENORIENTATION, "string", packageName);
		screenOrientation = (id == 0) ? "auto" : r.getString(id);

		id = r.getIdentifier(STRING_TITLEBAR, "string", packageName);
		titleBar = (id == 0) ? true : (r.getString(id).equalsIgnoreCase("false") ? false : true);
		id = r.getIdentifier(STRING_STATUSBAR, "string", packageName);
		statusBar = (id == 0) ? true : (r.getString(id).equalsIgnoreCase("false") ? false : true);
		id = r.getIdentifier(STRING_FULLSCREEN, "string", packageName);
		fullScreen = (id == 0) ? false : (r.getString(id).equalsIgnoreCase("true") ? true : false);
		id = r.getIdentifier(STRING_PROGRESSCOVER, "string", packageName);
		progressCover = (id == 0) ? true : (r.getString(id).equalsIgnoreCase("false") ? false : true);
		id = r.getIdentifier(STRING_BROWSERPROGRESSDIALOG, "string", packageName);
		browserProgressDialog = (id == 0) ? true : (r.getString(id).equalsIgnoreCase("false") ? false : true);
		id = r.getIdentifier(STRING_BROWSERBACKGROUNDCOLOR, "string", packageName);
		browserBackgroundColor = (id == 0) ? "#FFFFFF" : r.getString(id);
		id = r.getIdentifier(STRING_BAIDUSTATISTIC, "string", packageName);
		baiduStatistic = (id == 0) ? false : (r.getString(id).equalsIgnoreCase("true") ? true : false);
	}

	public static int getIconId(Context context) {
		return context.getResources().getIdentifier("icon", "drawable", context.getPackageName());
	}

	public static boolean isEnabled(Context context, String name, boolean defaultValue) {
		Resources r = context.getResources();
		String packageName = context.getPackageName();
		return isEnabled(r, packageName, name, defaultValue);
	}
	public static boolean isEnabled(Resources r, String packageName, String name, boolean defaultValue) {
		int id = r.getIdentifier(name, "string", packageName);
		if (id == 0) return defaultValue;
		else {
			if (defaultValue) {
				return !r.getString(id).equalsIgnoreCase("false");
			} else {
				return r.getString(id).equalsIgnoreCase("true");
			}
		}
	}

}
